<?php session_start(); require_once('../connections/con_gl.php'); ?>
<?php
$act = $_GET[act];
$url = "index.php?component=delivery";
$ope = $_POST['open'];
$rep = intval($_POST['replacement']);
if($rep=='1') {
	$url .= "&replace=1";	
}
/* Validasi */
if (trim($_POST['tanggal']) == '') {
	$error[] = '- Tanggal harus diisi';
}
if (trim($_POST['kendaraan']) == '') {
	$error[] = '- Kendaraan harus diisi';
}
if (trim($_POST['tanggal']) == '') {
	$error[] = '- Tanggal harus diisi';
}
if (trim($_POST['driver']) == '') {
	$error[] = '- Karyawan ( Driver ) harus diisi';
}
/*End validasi */
if (isset($error)) {
	echo "<img src=\"images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:red;\">Error : </b> <br />".implode("<br />", $error);
}else{
	if($act =='add') {
		$today = date("Y-m-d"); 
		if($_POST['rdcust']=='R')
			$query = "INSERT INTO gl_kkeluar (id, tgl, noref, kendaraan, driver, info, tipecustomer,created_by,created_at) VALUES (NULL, '$_POST[tanggal]', '$_POST[noreferensi]', '$_POST[kendaraan]', '$_POST[driver]', '$_POST[keterangan]', 'R','$_SESSION[nama]','$today')";
		else	
			$query = "INSERT INTO gl_kkeluar (id, tgl, noref, kendaraan, driver, info, tipecustomer,created_by,created_at) VALUES (NULL, '$_POST[tanggal]', '$_POST[nokontrak]', '$_POST[kendaraan]', '$_POST[driver]', '$_POST[keterangan]', 'C','$_SESSION[nama]','$today')";
			$sql = "select * from gl_kontrak where no='$_POST[nokontrak]'";
			$data = mysql_query($sql, $con_gl) or die(mysql_error());
			$row = mysql_fetch_assoc($data);
			
			$updatecheckoutin = mysql_query("update gl_retailtrans set checkin='$row[sampai]',checkout='$row[mulai]' where kontrak='$_POST[nokontrak]'", $con_gl) or die(mysql_error());
			
	$update_a = mysql_query("update gl_masterkendaraan set tersedia='0' where nopolisi='$_POST[kendaraan]'", $con_gl) or die(mysql_error());
	}else if($act=='edit'){
		if($_POST['rdcust']=='R')
	$query ="UPDATE gl_kkeluar SET tgl='$_POST[tanggal]', noref='$_POST[noreferensi]', driver='$_POST[driver]', info='$_POST[keterangan]' , kendaraan='$_POST[kendaraan]', updated_by='$_SESSION[nama]' WHERE id='$_POST[id]'";
		else	
	$query ="UPDATE gl_kkeluar SET tgl='$_POST[tanggal]', noref='$_POST[nokontrak]', driver='$_POST[driver]', info='$_POST[keterangan]' , kendaraan='$_POST[kendaraan]', updated_by='$_SESSION[nama]' WHERE id='$_POST[id]'";

	// --- update stok

	$update_a = mysql_query("update gl_masterkendaraan set tersedia='0' where nopolisi='$_POST[kendaraan]'", $con_gl) or die(mysql_error());
	//--- $update_b = mysql_query("update gl_masterkendaraan set tersedia='1' where nopolisi='$_POST[kendaraan_db]'", $con_gl) or die(mysql_error());
	// ---
	}else{
	$query = "delete from gl_kkeluar where id='$_POST[id]'";
	$update_a = mysql_query("update gl_masterkendaraan set tersedia='1' where nopolisi='$_POST[kendaraan]'", $con_gl) or die (mysql_error()); 
	}
	mysql_select_db($database_con_gl, $con_gl);
	$runquery = mysql_query($query, $con_gl) or die (mysql_error());
	if($runquery) {
		echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil ";if($act=='add' OR $act=='edit') { echo "disimpan";}else{ echo "dihapus"; } echo " ...";
	}else{
		echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
	}
}
?>
<?php if($runquery AND $ope=='window') { ?>
	<script type="text/javascript">top.opener.sendRespondLokasi();self.close(2000);</script>
<?php }else if($runquery AND $ope==''){ ?>
	<script type="text/javascript">setTimeout("location.href='<?php echo $url;?>'", 2000);</script>
<?php } ?>